package net.monare.mifss.admin.ui.win;

import com.vaadin.ui.*;
import com.vaadin.ui.themes.Runo;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.ApiKeyUiModule;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.evn.AddNewApiKeyListener;
import net.monare.mifss.evn.UpdateApiKeyListener;
import net.monare.mifss.exceptions.StorageServiceException;

public class ConfigureApiKeyWindow extends Window {

    //private NativeButton apiKeyButton

    public ConfigureApiKeyWindow(ConfigureStorageWindow parent, ApiKey apiKey, StorageDef storageDef){

        ApiKeyUiModule akui = null;
        NativeButton apiKeyButton = null;
        Label rpTitle = null;

        VerticalLayout verticalLayout = new VerticalLayout();
        verticalLayout.setSpacing(true);
        verticalLayout.setMargin(true);
        verticalLayout.setSizeFull();

        VerticalLayout formBar = new VerticalLayout();
        VerticalLayout buttonBar = new VerticalLayout();

        if (apiKey != null) {

            rpTitle = new Label(MifssMessages.getMessage("tile.api.key.configuration"));
            akui = new ApiKeyUiModule(apiKey);
            akui.protectFields();
            apiKeyButton = new NativeButton(MifssMessages.getMessage("button.update.new.key"));
            apiKeyButton.addClickListener(new UpdateApiKeyListener(storageDef, akui, parent, this));

        } else {

            rpTitle = new Label(MifssMessages.getMessage("tile.api.key.add"));
            akui = new ApiKeyUiModule();

            apiKeyButton = new NativeButton(MifssMessages.getMessage("button.add.new.key"));
            apiKeyButton.addClickListener(new AddNewApiKeyListener(storageDef, akui, parent, this));




        }

        rpTitle.setStyleName(Runo.LABEL_H1);

        formBar.addComponent(rpTitle);
        formBar.addComponent(akui);
        buttonBar.addComponent(apiKeyButton);

        buttonBar.setComponentAlignment(apiKeyButton, Alignment.BOTTOM_RIGHT);
        verticalLayout.addComponent(formBar);
        verticalLayout.addComponent(buttonBar);

        verticalLayout.setComponentAlignment(buttonBar, Alignment.BOTTOM_RIGHT);

        setStyleName(Runo.WINDOW_DIALOG);
        setWidth("550px");
        setHeight("350px");
        setModal(true);
        setContent(verticalLayout);


    }

}
